package com.zhuss.product.processor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.product.line.SearchLineBo;
import com.zhuss.bean.unit.linesite.BoardBean;
import com.zhuss.bean.vo.product.line.DetailLineVo;
import com.zhuss.bean.vo.product.line.ListLineSiteVo;
import com.zhuss.bean.vo.product.line.ListLineVo;
import com.zhuss.bean.vo.product.site.DetailSiteVo;
import com.zhuss.common.ListVo;
import com.zhuss.common.PageVo;
import com.zhuss.enums.IsEnum;
import com.zhuss.enums.SystemEnum;
import com.zhuss.exception.DzcxException;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.product.db.dao.ext.LineDao;
import com.zhuss.product.db.dao.ext.LineSiteDao;
import com.zhuss.product.db.dao.ext.PrelineSignDao;
import com.zhuss.product.db.dao.ext.SiteDao;
import com.zhuss.product.db.entity.Line;
import com.zhuss.product.db.entity.Site;
import com.zhuss.product.processor.cache.LineCache;
import com.zhuss.product.processor.cache.LineSiteCache;
import com.zhuss.product.processor.cache.SiteCache;
import com.zhuss.util.DateUtil;
import com.zhuss.util.JsonUtil;


/**
 * @description: 线路读
 * @author: zhushanshuo
 * @create: Wed Dec 30 13:08:49 CST 2020
 **/
@Component("lineRead")
public class LineRead {

	@Autowired
	private LineDao lineDao;

	@Autowired
	private LineSiteDao lineSiteDao;

	@Autowired
	private SiteDao siteDao;
	
	@Autowired
	private PrelineSignDao prelineSignDao;
	
	@Autowired
	private LineCache lineCache;
	
	@Autowired
	private SiteCache siteCache;
	
	@Autowired
	private LineSiteCache lineSiteCache;


	public Map<String, Line> listMap() {
		List<Line> list = lineDao.listAll();
		Map<String, Line> map = new HashMap<>();
		for (Line c : list) {
			map.put(c.getName(), c);
		}
		return map;
	}

	/**
	 * 线路
	 * @return
	 */
	public List<Line> listLine() {
		List<Line> result = lineDao.listAll();
		return result;
	}

	/**
	 * 线路
	 * @return
	 */
	public Object listLineSelect() {
		List<Line> lineList = listLine();
		List<ListVo> result = new ArrayList<>();
		transferList(lineList, result);
		return result;
	}


	/**
	 * 线路
	 * @return
	 */
	public Object listLine(List<Long> lineIdList) {
		List<Line> result = lineDao.listById(lineIdList);
		return result;
	}


	/**
	 * 根据名字查询线路
	 * @param
	 * @return
	 */
	public List<Line> list(String name, String no) {
		List<Line> result = lineDao.listByName(name, no);
		return result;
	}

	public List<Line> listNotId(String name, Long lineId, String no) {
		List<Line> result = new ArrayList<Line>();
		List<Line> list = list(name, no);
		for (Line c : list) {
			if (!c.getLineId().equals(lineId)) {
				result.add(c);
				break;
			}
		}
		return result;
	}

	/**
	 * 线路
	 * @return
	 */
	public Object listLineId() {
		List<Line> lineList = lineDao.listAll();
		List<Long> result = new ArrayList<>();
		transferListId(lineList, result);
		return result;
	}

	private void transferListId(List<Line> lineList, List<Long> result) {
		for (Line item : lineList) {
			result.add(item.getLineId());
		}
	}

	private void transferList(List<Line> lineList, List<ListVo> result) {
		for (Line item : lineList) {
			ListVo vo = new ListVo();
			vo.setKey(item.getLineId());
			vo.setLabel(item.getName());
			result.add(vo);
		}

	}

	/**
	 * 线路列表
	 * @param bo
	 * @return
	 * @throws DzcxException 
	 */
	public Object list(SearchLineBo bo) throws DzcxException {
		PageVo<ListLineVo> result = new PageVo<ListLineVo>();
        result.setPageNum(bo.getPageNum());
        result.setPageSize(bo.getPageSize());
        result.setItems(transfer(lineDao.list(bo), bo));
        result.setTotal(lineDao.countList(bo));
		return result;
	}

	private List<ListLineVo> transfer(List<ListLineVo> list, SearchLineBo bo) throws DzcxException {
		Integer system = bo.getConcurrentSystem();
		Long userId = bo.getConcurrentUserId();
		for (ListLineVo vo : list) {
			vo.setApplyCount(prelineSignDao.countByPrelineId(vo.getLineId()));
			if (SystemEnum.APP.sameKey(system)) {
				Long prelineId = vo.getLineId();
				Integer userCount = prelineSignDao.countUserApply(userId, prelineId);
				if (userCount > 0) {
					vo.setIsApply(IsEnum.是.key());
				}
				
				initLineSite(vo);
				
			}
		}
		return list;
	}
	
	public void initLineSite(ListLineVo vo) throws DzcxException {
		if (vo.getLineId() == null || vo.getLineId() == 0) {
            throw new DzcxException(ExceptionCode.线路ID为空);
        }
        DetailLineVo lineVo = lineCache.get(vo.getLineId());
        List<ListLineSiteVo> siteList = lineSiteCache.get(vo.getLineId());
        vo.setSiteList(siteList);
        Long startPrice = 0l;
        Long endPrice = 0l;
        Integer arriveTime = 0;
        int i = 0;
        for (ListLineSiteVo site : siteList) {
            DetailSiteVo siteVo = siteCache.get(site.getSiteId());
            site.setSiteName(siteVo.getName());
            site.setLng(siteVo.getLng());
            site.setLat(siteVo.getLat());
            if (i == siteList.size() - 1) {
                endPrice = site.getPrice();
            }
            arriveTime = arriveTime + site.getNearestDuration();
            site.setArriveTime(DateUtil.minTime(arriveTime, lineVo.getBoardList().get(0).getTime()));
            i ++;
        }
        vo.setPrice((endPrice - startPrice) == 0l ? 100 : (endPrice - startPrice));
	}

	/**
	 * 线路详情
	 * @param lineId
	 * @return
	 * @throws DzcxException
	 */
	public DetailLineVo detail(Long lineId) throws DzcxException {
		DetailLineVo result = lineDao.detail(lineId);
		if (result == null) {
			throw new DzcxException(ExceptionCode.数据不存在);
		}
		return result;
	}

	/**
	 * 根据线路查询绑定的站点
	 * @param lineId
	 * @return
	 * @throws DzcxException
	 */
	public Object bindSiteList(Long lineId) throws DzcxException {
		List<ListLineSiteVo> res = lineSiteDao.listByLineId(lineId);
		if (res == null) {
			throw new DzcxException(ExceptionCode.数据不存在);
		} else {
			res.forEach(siteVo -> {
				Site site = siteDao.selectByPrimaryKey(siteVo.getSiteId());
				siteVo.setLng(site.getLng());
				siteVo.setLat(site.getLat());
				siteVo.setName(siteDao.selectByPrimaryKey(siteVo.getSiteId()).getName());
			});
		}
		return res;

	}


	/**
	 * 根据lineId查询
	 * @param lineId
	 * @return
	 * @throws DzcxException
	 */
	public Object queryBoardByLineId(Long lineId) throws DzcxException {
		DetailLineVo vo = lineDao.detail(lineId);
		if (vo == null) {
			throw new DzcxException(ExceptionCode.数据不存在);
		} else {
			List<BoardBean> beans = new ArrayList<>();
			if (vo.getBoard() != null) {
				beans = JsonUtil.fromJsonList(vo.getBoard(), BoardBean.class);
			}
			return beans;
		}
	}
	
	/**
	 * 查询所有班次模板  剔除新线招募
	 * @return
	 */
	public List<Line> queryAllList(Integer pretype) {
		List<Line> result = lineDao.listByPretype(pretype);
	    return result;
	}

	/**
	 * 查询所有
	 * @return
	 *//*
	public Object queryAllList() {
	    return lineCache.getList();
	}*/

}
